package com.network.controller;

import com.google.gson.Gson;
import com.network.entity.Menu;
import com.network.entity.Role;
import com.network.entity.User;
import com.network.qo.UserQo;
import com.network.service.UserService;
import com.network.util.CommonUtils;
import com.network.vo.MenuVo;
import com.network.vo.PageResult;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
 * @author peng
 */
@RestController
public class UserController
{

    Logger logger= LoggerFactory.getLogger(UserController.class);
    PageResult pageResult=null;

    @Autowired
    UserService userService;


    @RequestMapping(value = "/menu",method = RequestMethod.GET,produces = "application/json;charset=utf-8;")
    public CompletableFuture<String> getMenu(Principal user) throws Exception{

        return userService.findByUserName(user.getName()).thenApply(usertemp ->{

            UserQo userQo = new UserQo();
            BeanUtils.copyProperties(usertemp,userQo);
            //分类列表（顶级菜单）
            List<MenuVo> kindList = new ArrayList<MenuVo>(10);
            List<Long> kindIds = new ArrayList<>();

            MenuVo menuVo=null;
            for(Role roleQo :userQo.getRoles()){
                for(Menu menu : roleQo.getMenus()){
                    //去重，获取分类列表
                    Long menuId = menu.getId();
                    if(! kindIds.contains(menuId)){

                        if (menu.getDelFlag()==0)
                        {
                            menuVo=new MenuVo();
                            BeanUtils.copyProperties(menu,menuVo);
                            kindList.add(menuVo);
                            kindIds.add(menuId);
                        }
                    }
                }
            }
            List<MenuVo> nodeTrees = new ArrayList<>();
            Map<Long, MenuVo> sysMenuMap= kindList.stream().collect(Collectors.toMap(MenuVo::getId, sysMenu -> sysMenu));
            for (MenuVo menu:kindList)
            {
                Long parent=menu.getParentId();
                if (menu.getType()==2)
                {
                    MenuVo parentTreeNode = sysMenuMap.get(parent);
                    if (parentTreeNode.getChildrens() == null) {
                        parentTreeNode.setChildrens(new ArrayList<>());
                    }
                    parentTreeNode.getChildrens().add(menu);
                    parentTreeNode.getChildrens().sort(Comparator.comparing(MenuVo::getOrderNum));
                }
            }

            //取根节点的树
            nodeTrees = sysMenuMap.values().stream().filter(menuVo1 -> menuVo1.getType()==1).collect(Collectors.toList());
            return  new Gson().toJson(nodeTrees);
        });
    }


    @RequestMapping(value = "/get/findUser",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
    @ResponseBody
    public PageResult getUserByPage(UserQo userQo)
    {

        Page<User> userPage= userService.findByPage(userQo);

        pageResult=new PageResult();
        pageResult.setCode(0);
        pageResult.setData(userPage.getContent());

        return pageResult;

    }

}
